package indoor.nexd.com.nexdmap.adapter;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import indoor.nexd.com.nexdmap.R;
import indoor.nexd.com.nexdmap.model.GridViewModel;

public class GridViewAdapter extends BaseAdapter {

    private Context context;
    private List<GridViewModel> gridViewModels;


    public GridViewAdapter(Context context, List<GridViewModel> gridViewModels) {
        this.context = context;
        this.gridViewModels = gridViewModels;
    }

    @Override
    public int getCount() {
        return gridViewModels.size();
    }

    @Override
    public GridViewModel getItem(int position) {
        return gridViewModels.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;

        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.night_view, null);
            viewHolder.imageView = (ImageView) convertView.findViewById(R.id.ItemImage);
            viewHolder.textView = (TextView) convertView.findViewById(R.id.ItemText);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.imageView.setBackground(context.getResources().getDrawable(getItem(position).getResourceId()));
        viewHolder.textView.setText(getItem(position).getItemName());
        return convertView;
    }

    static class ViewHolder {
        ImageView imageView;
        TextView textView;
    }

}